package com.youan.controller;

import com.youan.bean.User;
import com.youan.service.UserService;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@RestController
public class MyController {
    @Resource
    UserService userService;

    @RequestMapping("/user/add/{name}/{age}")
    public String userAdd(@PathVariable("name") String name,
                          @PathVariable("age") String age) {
        User user = new User();
        user.setName(name);
        user.setAge(Integer.valueOf(age));
        return userService.addUser(user);
    }

    @RequestMapping("/user/select/all")
    public String userSelectAll() {
        List<User> users = userService.listAllUsers();
        StringBuilder stringBuilder = new StringBuilder();
        for (User user : users) {
            stringBuilder.append(users.toString() + "\n");
        }
        return stringBuilder.toString();
    }

    @RequestMapping("/user/select/name/{name}")
    public String userSelectByName(@PathVariable String name) {
        Map<Integer, Object> map = userService.selectUserByRName(name);
        String s1 = map.get(1).toString();
        String s2 = map.get(2).toString();
        return s1 + ", " + s2;
    }
}
